package com.lzc.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONUtil;
import com.lzc.dto.AccessToken;
import com.lzc.dto.UserInfo;
import org.apache.commons.httpclient.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/login")
public class WxLoginController {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/code")
    public String getCode(
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam("echostr") String echostr

    ){


        System.out.println("signature: "+signature);
        System.out.println("timestamp: "+timestamp);
        System.out.println("nonce: "+nonce);
        System.out.println("echostr: "+echostr);

        return echostr;
    }

    /**
     *  获取code
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/qrcode")
    public void getQRCode() throws FileNotFoundException, UnsupportedEncodingException {

        String encode = URLEncoder.encode("http://6b2096e4.r6.cpolar.top/login/back","UTF-8");

        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=wx7c92d6a7a31cdb4d" +
                "&redirect_uri="+encode +
                "&response_type=code" +
                "&scope=snsapi_userinfo" +
                "&state=STATE#wechat_redirect";

        try(FileOutputStream fileOutputStream = new FileOutputStream("D://1.jpg")) {

            QrCodeUtil.generate(url,300,300,"jpg",
                    fileOutputStream);

            System.out.println("创建二维码成功");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 扫码后微信服务器回调
     * @param code
     */
    @RequestMapping("/back")
    public void getKeyToken(@RequestParam("code") String code){

        System.out.println("code:  "+code);


        // 换取access_token
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
                "appid=wx7c92d6a7a31cdb4d" +
                "&secret=4c41eadb8260f6d7e48f0bec5583ef28" +
                "&code="+code+
                "&grant_type=authorization_code";


        String jsonToken = restTemplate.getForObject(url, String.class);

        System.out.println("jsonToken: "+ jsonToken);

        AccessToken accessToken = JSONUtil.toBean(jsonToken, AccessToken.class);

        System.out.println("accessToken:  "+accessToken);

        // 根据access_key 和 openid 获取用户信息
        String userUrl = "https://api.weixin.qq.com/sns/userinfo?" +
                "access_token="+accessToken.getAccess_token()+
                "&openid="+accessToken.getOpenid()+
                "&lang=zh_CN";

        String  jsonUser = restTemplate.getForObject(userUrl, String.class);

        System.out.println("---------------------------------------------------------");
        System.out.println("jsonUser: "+jsonUser);
        System.out.println("---------------------------------------------------------");

        UserInfo user = JSONUtil.toBean(jsonUser, UserInfo.class);

        System.out.println(user);


    }


}
